package com.bailan.foreat.service.impl;

import com.bailan.foreat.entity.Seller;
import com.bailan.foreat.mapper.SellerMapper;
import com.bailan.foreat.service.SellerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ste
 * @since 2023-02-21 07:50:43
 */
@Service
public class SellerServiceImpl extends ServiceImpl<SellerMapper, Seller> implements SellerService {

    @Autowired
    SellerMapper sellerMapper;

    @Override
    public void register(String sellerName, String sellerPassword, String rePassword, String sellerImg) {
        QueryWrapper<Seller> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seller_name", sellerName);
        Seller seller = sellerMapper.selectOne(queryWrapper);
        if (seller!=null){
            throw new RuntimeException("用户名已存在");
        }
        if (StringUtils.isEmpty(sellerPassword)||StringUtils.isEmpty(rePassword)||!sellerPassword.equals(rePassword)){
            throw new RuntimeException("两次密码输入不一致");
        }
        Seller newSeller = new Seller();
        newSeller.setSellerName(sellerName);
        newSeller.setSellerPassword(sellerPassword);
        newSeller.setSellerImg(sellerImg);

        sellerMapper.insert(newSeller);
    }

    @Override
    public void login(String sellerName, String sellerPassword) {
        QueryWrapper<Seller> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seller_name",sellerName);
        Seller seller = sellerMapper.selectOne(queryWrapper);
        if(seller==null){
            throw new RuntimeException("用户名不存在");
        }
        if(!seller.getSellerPassword().equals(sellerPassword)){
            throw new RuntimeException("您输入的密码不正确");
        }
    }

    @Override
    public Seller getCurrentSeller(String account) {

        QueryWrapper<Seller> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sellerName",account);

        Seller seller = sellerMapper.selectOne(queryWrapper);
        return seller;

    }

    @Override
    public Seller getSellerBySellerName(String sellerName) {
        QueryWrapper<Seller> wrapper = new QueryWrapper<>();
        wrapper.eq("seller_name", sellerName);
        Seller seller = sellerMapper.selectOne(wrapper);

        return seller;
    }

    @Override
    public void changePassword(String sellerOldPassword, String sellerPassword, String rePassword, String currentSellerName) {
        QueryWrapper<Seller> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seller_name", currentSellerName);
        Seller seller = sellerMapper.selectOne(queryWrapper);
        if (!seller.getSellerPassword().equals(sellerOldPassword)){
            throw new RuntimeException("您输入的密码错误");
        }
        if (StringUtils.isEmpty(sellerPassword)||StringUtils.isEmpty(rePassword)||!sellerPassword.equals(rePassword)){
            throw new RuntimeException("两次新密码输入不一致");
        }
        Seller newSeller = new Seller();
        newSeller.setSellerName(seller.getSellerName());
        newSeller.setSellerPassword(sellerPassword);
        newSeller.setSellerImg(seller.getSellerImg());
        sellerMapper.update(newSeller,queryWrapper);
    }
}
